/******************************************************************************
 * Copyright (C) 2015  671643387@qq.com
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 *****************************************************************************/

#include "NodeCpp/Crypto/BigNumber.h"
#include <memory.h>
#include <cassert>
#include <new>
#include <algorithm>
#include <openssl/bn.h>
#include "NodeCpp/Macros.h"

namespace NodeCpp {
namespace Crypto {

		BigNumber::BigNumber(void)
            : BN_(BN_new())
            , Array_()
		{

		}

		BigNumber::BigNumber(const BigNumber& _X)
			: BN_(BN_dup(_X.BN_))
			, Array_()
		{

		}

		BigNumber::BigNumber(std::uint32_t _X)
			: BN_(BN_new())
			, Array_()
		{
			BN_set_word(BN_, _X);
		}

		BigNumber::~BigNumber(void)
		{
			BN_free(BN_);
		}

		void BigNumber::SetUInt32(std::uint32_t _X)
		{
			BN_set_word(BN_, _X);
		}

		void BigNumber::SetUInt64(std::uint64_t _X)
		{
			BN_add_word(BN_, (std::uint32_t)(_X >> 32));
			BN_lshift(BN_, BN_, 32);
			BN_add_word(BN_, (std::uint32_t)(_X & 0xFFFFFFFF));
		}

		void BigNumber::SetBinary(const std::uint8_t* _Bytes, int _Len)
		{
			std::uint8_t _T[1000];
			for (int _I = 0; _I < _Len; ++_I) {
				_T[_I] = _Bytes[_Len - 1 - _I];
			}
			BN_bin2bn(_T, _Len, BN_);
		}

		void BigNumber::SetHexStr(const char* _Str)
		{
			BN_hex2bn(&BN_, _Str);
		}

		void BigNumber::SetRand(int _Numbits)
		{
			BN_rand(BN_, _Numbits, 0, 1);
		}

		void BigNumber::Free(void* _X)
		{
			OPENSSL_free(_X);
		}

		BigNumber BigNumber::operator=(const BigNumber& _X)
		{
			BN_copy(BN_, _X.BN_);
			return *this;
		}

		BigNumber BigNumber::operator+=(const BigNumber& _X)
		{
			BN_add(BN_, BN_, _X.BN_);
			return *this;
		}

		BigNumber BigNumber::operator-=(const BigNumber& _X)
		{
			BN_sub(BN_, BN_, _X.BN_);
			return *this;
		}

		BigNumber BigNumber::operator*=(const BigNumber& _X)
		{
			BN_CTX* _Bnctx;

			_Bnctx = BN_CTX_new();
			BN_mul(BN_, BN_, _X.BN_, _Bnctx);
			BN_CTX_free(_Bnctx);

			return *this;
		}

		BigNumber BigNumber::operator/=(const BigNumber& _X)
		{
			BN_CTX* _Bnctx;

			_Bnctx = BN_CTX_new();
			BN_div(BN_, NULL, BN_, _X.BN_, _Bnctx);
			BN_CTX_free(_Bnctx);

			return *this;
		}

		BigNumber BigNumber::operator%=(const BigNumber& _X)
		{
			BN_CTX* _Bnctx;

			_Bnctx = BN_CTX_new();
			BN_mod(BN_, BN_, _X.BN_, _Bnctx);
			BN_CTX_free(_Bnctx);

			return *this;
		}

		BigNumber BigNumber::Exp(const BigNumber& _Bn)
		{
			BigNumber _Ret;
			BN_CTX* _Bnctx;

			_Bnctx = BN_CTX_new();
			BN_exp(_Ret.BN_, BN_, _Bn.BN_, _Bnctx);
			BN_CTX_free(_Bnctx);

			return _Ret;
		}

		BigNumber BigNumber::ModExp(const BigNumber& _Bn1, const BigNumber& _Bn2)
		{
			BigNumber _Ret;
			BN_CTX* _Bnctx;

			_Bnctx = BN_CTX_new();
			BN_mod_exp(_Ret.BN_, BN_, _Bn1.BN_, _Bn2.BN_, _Bnctx);
			BN_CTX_free(_Bnctx);

			return _Ret;
		}

		int BigNumber::GetNumBytes(void)
		{
			return BN_num_bytes(BN_);
		}

		uint32_t BigNumber::AsUInt32(void)
		{
			return (std::uint32_t)BN_get_word(BN_);
		}

		bool BigNumber::IsZero(void) const
		{
			return BN_is_zero(BN_) != 0;
		}

		uint8_t* BigNumber::AsByteArray(int _MinSize)
		{
			int _Length = NODECPP_MAX(_MinSize, GetNumBytes());
			Array_.reset(new (std::nothrow) std::uint8_t[_Length]);
			NODECPP_ASSERT(Array_);
			if (!Array_) {
				return NULL;
			}
			if (_Length > GetNumBytes()) {
				memset((void*)Array_.get(), 0, _Length);
			}
			BN_bn2bin(BN_, (unsigned char*)Array_.get());
			std::reverse(Array_.get(), Array_.get() + _Length);
			return Array_.get();
		}

		const char* BigNumber::AsHexStr(void)
		{
			return BN_bn2hex(BN_);
		}

		const char* BigNumber::AsDecStr(void)
		{
			return BN_bn2dec(BN_);
		}

}}